/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chat;

import Core.MessageBuilder;
import Core.MessageType;
import Core.ReaderThread;
import Core.StatusType;
import Core.StringHelper;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Mr Bao
 */
public class ChatForm extends Canvas implements Runnable {

    // <editor-fold defaultstate="collapsed" desc="Khai bao">
    private Sprite online, offline, collapsed, expanded, emotion, busy, ide, mobile;
    private int statusId = 0;
    private String keyword;
    private String title = "Yahoo";
    private Vector tabHeaders = new Vector();
    private int maxTab;
    private int currentTab;
    private int heightList = 0;
    private Group group;
    private String strNickName = "";
    private int xNick;
    private int yNick;
    private Nick nick;
    private String[] listYahooCur;
    private int statusIDMove = 0;
    private byte tickerInterval = 0;
    private boolean isShowOnlyOnline = false;
    private Vector yahoo = new Vector(), yahooTemp;
    private int minListYahoo = 0, heightListYahoo, curListYahoo = 0, listYahooSize = 0;
    private int statusLenShow = 0;
    private boolean isSearchNick;
    private int pointer;
    private boolean isShowPointer;
    // </editor-fold>

    public ChatForm() {
        new Thread(this).start();
    }

    protected void paint(Graphics g) {
        setFullScreenMode(true);
        Util.ClearScreen(g, getWidth(), getHeight());
        heightListYahoo = getHeight() / Util.FontSmall.getHeight() - 3;
        Util.DrawBottomBar(g, "", title, "", getWidth(), getHeight());
        DrawTab(g);
    }

    private void DrawYahooList(Graphics g) {
        if (currentTab == 1) {
            int count = 0;
            heightList = 0;
            Sprite sprite, sprite1;
            listYahooSize = 0;
            listYahooCur = new String[heightListYahoo];
            while (yahoo.size() > count) {
                group = (Group) yahoo.elementAt(count);
                int count1 = 0;
                int count2 = 0;
                if (heightList + minListYahoo >= 0 && heightList + minListYahoo < heightListYahoo) {
                    if (group.isExpanded) {
                        sprite = expanded;
                    } else {
                        sprite = collapsed;
                    }
                    if (sprite != null) {
                        sprite.setPosition(getWidth() / 80, Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 4);
                        sprite.paint(g);
                    }
                    if (minListYahoo + heightList == curListYahoo) {
                        g.setColor(255, 0, 0);
                    } else {
                        g.setColor(0);
                    }
                    g.drawString(group.nameGroup, getWidth() / 80 + 10, Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 4 - (Util.FontSmall.getHeight() - 6) / 2, 0);
                    g.drawString("(" + group.GetOnline() + "/" + group.nicks.size() + ")", getWidth() / 80 + 15 + Util.FontSmall.stringWidth(group.nameGroup), Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 4 - (Util.FontSmall.getHeight() - 6) / 2, 0);
                    listYahooCur[heightList + minListYahoo] = "" + count;
                }
                heightList++;
                while (group.nicks.size() > count1 && group.isExpanded) {
                    nick = (Nick) group.nicks.elementAt(count1);
                    if ((nick.status != StatusType.OFFLINE) && ((nick.status != StatusType.INVISIBLE))) {
                        sprite = online;
                        if ((nick.status == StatusType.BUSY) || (nick.status == StatusType.NOTATDESK) || (nick.status == StatusType.NOTATHOME) || (nick.status == StatusType.NOTINOFFICE) || (nick.status == StatusType.OUTTOLUNCH) || (nick.status == StatusType.STEPPEDOUT)) {
                            sprite1 = busy;
                        } else if (nick.status == StatusType.IDLE) {
                            sprite1 = ide;
                        } else if (nick.status == StatusType.ONPHONE) {
                            sprite1 = mobile;
                        } else {
                            sprite1 = null;
                        }
                    } else {
                        sprite = offline;
                        sprite1 = null;
                    }
                    if (!isShowOnlyOnline || (isShowOnlyOnline && nick.status != StatusType.INVISIBLE && nick.status != StatusType.OFFLINE) && ((heightList + minListYahoo) >= 0) && ((heightList + minListYahoo) < heightListYahoo)) {
                        if (sprite != null) {
                            sprite.setPosition(getWidth() / 30, Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 1);
                            sprite.paint(g);
                        }
                        if (sprite1 != null) {
                            sprite1.setPosition(getWidth() / 30 - 10, Util.fontMedium.getHeight() * (heightList + minListYahoo + 2));
                            sprite1.paint(g);
                        }
                        String nameDisplay = nick.nickName;
                        if (nick.fullName != null && !nick.fullName.equals("")) {
                            nameDisplay = nick.fullName;
                        }
                        if (minListYahoo + heightList == curListYahoo) {
                            g.setColor(255, 0, 0);
                            g.drawString(nameDisplay, getWidth() / 20 + 10, Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 2 - (Util.FontSmall.getHeight() - 6) / 2, 0);
                            if (nick.fullName != null && !nick.fullName.equals("")) {
                                strNickName = nick.nickName;
                                xNick = getWidth() - Util.FontSmall.stringWidth(strNickName) - 10;
                                yNick = Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 2 - (Util.FontSmall.getHeight() - 6) / 2;
                            } else {
                                strNickName = "";
                            }
                            if (nick.statusMsg.length() > 0) {
                                if (Util.FontSmall.stringWidth(nameDisplay) < getWidth() - 10) {
                                    statusLenShow = (getWidth() - (getWidth() / 20 + 10 + Util.FontSmall.stringWidth(nameDisplay) - 4)) / Util.FontSmall.stringWidth("a");
                                    g.setColor(0);
                                    if (nick.statusMsg.length() > statusLenShow) {
                                        int end;
                                        if (nick.statusMsg.length() - statusIDMove >= statusLenShow) {
                                            end = statusIDMove + statusLenShow;
                                        } else {
                                            end = nick.statusMsg.length();
                                        }
                                        if (statusIDMove >= 0 && statusIDMove < nick.statusMsg.length() && end >= 0 && end < nick.statusMsg.length()) {
                                            g.drawString(" - " + nick.statusMsg.substring(statusIDMove, end), getWidth() / 20 + 10 + Util.FontSmall.stringWidth(nameDisplay), Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 2 - (Util.FontSmall.getHeight() - 6) / 2, 0);
                                        }
                                        if (tickerInterval > 1) {
                                            statusIDMove++;
                                        }
                                        if (statusIDMove > nick.statusMsg.length() - statusLenShow) {
                                            statusIDMove = 0;
                                        }
                                    } else {
                                        g.drawString(" - " + nick.statusMsg, getWidth() / 20 + 10 + Util.FontSmall.stringWidth(nameDisplay), Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 2 - (Util.FontSmall.getHeight() - 6) / 2, 0);
                                    }
                                }
                            } else {
                                g.setColor(0);
                                g.drawString(nameDisplay, getWidth() / 20 + 10, Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 2 - (Util.FontSmall.getHeight() - 6) / 2, 0);
                                if (nick.statusMsg.length() > 0) {
                                    g.setColor(0, 0, 0);
                                    g.drawString(" - " + nick.statusMsg, getWidth() / 20 + 10 + Util.FontSmall.stringWidth(nameDisplay), Util.fontMedium.getHeight() * (heightList + minListYahoo + 2) + 2 - (Util.FontSmall.getHeight() - 6) / 2, 0);
                                }
                            }
                            listYahooCur[heightList + minListYahoo] = "N," + nick.nickName;
                        }
                        if ((!isShowOnlyOnline || (isShowOnlyOnline && nick.status != StatusType.INVISIBLE && nick.status != StatusType.OFFLINE))) {
                            count2++;
                            heightList++;
                        }
                        count1++;
                    }
                    listYahooSize += count2;
                    listYahooSize++;
                    count++;
                }
                if (yahoo.isEmpty()) {
                    strNickName = "";
                }
                if (strNickName.length() > 0) {
                    Vector v = new Vector();
                    if (Util.FontSmall.stringWidth(strNickName) < getWidth() - xNick - 5) {
                        v.addElement(strNickName);
                    } else {
                        int id = strNickName.length() - 1;
                        while (true) {
                            if (id >= 0 && id < strNickName.length()) {
                                if (Util.fontLinkWeb.stringWidth(strNickName) < getWidth() - getWidth() / 40 - 4) {
                                    v.addElement(strNickName);
                                    break;
                                }
                                if (Util.fontLinkWeb.stringWidth(strNickName.substring(0, id)) < getWidth() - getWidth() / 40 - 4) {
                                    v.addElement(strNickName.substring(0, id));
                                    strNickName = strNickName.substring(id);
                                    id = strNickName.length() - 1;
                                } else {
                                    id--;
                                }
                            } else {
                                break;
                            }
                        }
                    }
                    if (yNick < getHeight() / 3) {
                        yNick += v.size() * Util.FontSmall.getHeight();
                    } else {
                        yNick -= v.size() * Util.FontSmall.getHeight();
                    }
                    g.setColor(0xffffff);
                    g.fillRect(xNick, yNick, Util.FontSmall.stringWidth(strNickName) + 4, Util.FontSmall.getHeight() * v.size());
                    g.setColor(0);
                    g.drawRect(xNick, yNick, Util.FontSmall.stringWidth(strNickName) + 4, v.size() * Util.FontSmall.getHeight());
                    g.drawString(strNickName, xNick + 2, yNick, 0);
                }
                if (isSearchNick) {
                    g.setColor(Util.color2);
                    g.drawRect(2, getHeight() - Util.fontMedium.getHeight() * 2 - 2, getWidth() - 4, Util.fontMedium.getHeight());
                    g.setColor(0xffffff);
                    g.fillRect(3, getHeight() - Util.fontMedium.getHeight() * 2 - 1, getWidth() - 5, Util.fontMedium.getHeight() - 1);
                    g.setFont(Util.fontChat);
                    if (Util.fontChat.stringWidth(keyword) > getWidth() - 35) {
                        String str = null;
                        int i;
                        if (Util.fontChat.substringWidth(keyword, 0, pointer) > getWidth() - 35) {
                            i = 0;
                            while (true) {
                                if (i >= 0 && i < keyword.length()) {
                                    str = keyword.substring(i, pointer);
                                    if (Util.fontChat.stringWidth(str) <= getWidth() - 35) {
                                        break;
                                    }
                                    i++;
                                } else {
                                    break;
                                }
                            }
                            g.drawString(str, 2, getHeight() - Util.fontChat.getHeight() * 2 - 2, 0);
                        } else {
                            i = keyword.length() - 1;
                            while (true) {
                                if (i >= 0 && i < keyword.length()) {
                                    str = keyword.substring(0, i);
                                    if (Util.fontChat.stringWidth(str) <= getWidth() - 35) {
                                        break;
                                    }
                                    i++;
                                } else {
                                    break;
                                }
                            }
                            g.drawString(str, 2, getHeight() - Util.fontChat.getHeight() * 2 - 2, 0);
                        }
                    } else {
                        g.drawString(keyword, 2, getHeight() - Util.fontChat.getHeight() * 2 - 2, 0);
                    }
                    if (isShowPointer) {
                        g.setColor(0);
//                        if (Util.fontChat.substringWidth(keyword, 0, pointer)>getWidth()-Util.FontSmall.stringWidth(title))
                    }
                }
            }
        }
    }

// <editor-fold defaultstate="collapsed" desc="Draw">
    private void DrawTab(Graphics g) {
        Util.GradientBox(g, Util.color1, Util.color2, 0, Util.fontMedium.getHeight() - 3, getWidth(), Util.fontMedium.getHeight() - 2, 0);
        String tabHeader = "";
        int strLen = 0;
        int startTab = 0;
        for (int i = 0; i < maxTab; i++) {
            tabHeader = "  " + tabHeaders.elementAt(i) + "  ";
            if ((i >= (currentTab - 1) && currentTab > 1) || currentTab == 1) {
                strLen += Util.FontSmall.stringWidth(tabHeader);
                if (currentTab == 1 && strLen > getWidth()) {
                    startTab = getWidth() - strLen - 5;
                    break;
                }
            }
        }
        strLen = startTab;
        int drawColor1;
        int drawColor2 = 0;
        for (int i = 0; i < maxTab; i++) {
            if (currentTab == i) {
                drawColor1 = Util.color11;
                drawColor2 = Util.color21;
            } else {
                drawColor1 = Util.color1;
                drawColor2 = Util.color2;
            }
            tabHeader = "  " + tabHeaders.elementAt(i) + "  ";
            if ((i >= (currentTab - 1) && currentTab > 0) || currentTab == 1) {
                FillTab(i, tabHeader, g, drawColor1, drawColor2, strLen, Util.fontMedium.getHeight() - 4, Util.fontMedium.getHeight() - 1, 0);
                strLen += Util.FontSmall.stringWidth(tabHeader);
            }
            if (i == 1) {
                Sprite statusIcon = online;
                switch (statusId + 1) {
                    case StatusType.AVAILABLE:
                    case StatusType.CUSTOM:
                    case StatusType.IDLE:
                        statusIcon = online;
                        break;
                    case StatusType.BUSY:
                    case StatusType.NOTATHOME:
                    case StatusType.NOTATDESK:
                    case StatusType.NOTINOFFICE:
                    case StatusType.ONPHONE:
                    case StatusType.OUTTOLUNCH:
                    case StatusType.STEPPEDOUT:
                        statusIcon = busy;
                        break;
                    case StatusType.INVISIBLE:
                        statusIcon = offline;
                        break;
                }
                statusIcon.setPosition(3, Util.fontMedium.getHeight() + 3);
                statusIcon.paint(g);
            }
        }
        g.setColor(drawColor2);
        g.drawLine(0, Util.fontMedium.getHeight() - 3, getWidth(), Util.fontMedium.getHeight() - 3);
    }// </editor-fold>

    public void FillTab(int tab, String tabHeader, Graphics g, int color1, int color2, int left, int top, int height, int orientation) {
        int tabSpace = 0;
        Util.GradientBox(g, color1 - 1, color2 - 1, left + tabSpace, top + 1, Util.FontSmall.stringWidth(tabHeader), height - 1, orientation);
        g.setColor(color2);
        g.fillRect(left + tabSpace, top + 1 + (height - 1) / 3, Util.FontSmall.stringWidth(tabHeader), (height - 1) * 2 / 3);
        g.setColor(color2);
        g.drawLine(left + tabSpace, top + height / 3, left + tabSpace, top + height - 1);
        g.drawLine(left + tabSpace + Util.FontSmall.stringWidth(tabHeader), top + height / 3, tabSpace + left + Util.FontSmall.stringWidth(tabHeader), top + height - 1);
        g.drawArc(tabSpace + left, top, height * 4 / 3, height * 2 / 3, 135, 45);
        g.drawArc(tabSpace + left + Util.FontSmall.stringWidth(tabHeader) - height * 4 / 3, top, height * 4 / 3, height * 2 / 3, 0, 45);
        if (currentTab == tab) {
            g.setColor(255, 255, 0);
        } else {
            g.setColor(210, 210, 210);
        }
        g.drawString(tabHeader, left + tabSpace, top + height / 8, 0);
    }
    private ReaderThread readerThread;

    public void run() {
        if (readerThread == null) {
            readerThread = Yahoo.slaveClient.GetReaderThread();
        }
        while (true) {
            if (!readerThread.GetQueue().isEmpty()) {
                String message = readerThread.GetQueue().elementAt(0).toString();
                readerThread.GetQueue().removeElementAt(0);
                ProcessMessage(message);
            }
            repaint();
        }
    }

    private void ProcessMessage(String message) {
        if (message.startsWith(MessageType.FriendList)) {
            GetFriendList(message);
        } else if (message.startsWith(MessageType.ListStatus)) {
            SetStatus(message);
        }
    }

    private void SetStatus(String message) {
        message = message.substring(message.indexOf(MessageBuilder.ONE) + 1);
        String[] content = StringHelper.split(message, ';');
        for (int i = 0; i < yahoo.size(); i++) {
            Group g = (Group) yahoo.elementAt(i);
            if (g.nameGroup.equals(content[0])) {
                for (int j = 0; j < g.nicks.size(); j++) {
                    Nick n = (Nick) g.nicks.elementAt(i);
                    if (n.nickName.equals(content[1])) {
                        n.statusMsg = content[3];
                        if (content[2].equals("CUSTOM")) {
                            n.status = StatusType.CUSTOM;
                        } else if (content[2].equals("AVAILABLE")) {
                            n.status = StatusType.AVAILABLE;
                        } else if (content[2].equals("OFFLINE")) {
                            n.status = StatusType.OFFLINE;
                        } else if (content[2].equals("BUSY")) {
                            n.status = StatusType.BUSY;
                        } else if (content[2].equals("IDLE")) {
                            n.status = StatusType.IDLE;
                        }
                    }
                    break;
                }
                break;
            }
        }
    }

    private void GetFriendList(String message) {
        Group g = new Group();
        message = message.substring(message.indexOf(MessageBuilder.ONE) + 1);
        String[] allNicks = StringHelper.split(message, ';');
        g.nameGroup = allNicks[0];
        g.isExpanded = true;
        for (int i = 0; i < allNicks.length; i++) {
            String string = allNicks[i];
            Nick n = new Nick();
            n.nickName = string;
            g.nicks.addElement(n);
        }
        yahoo.addElement(g);
    }
}
